﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;
using System.Data.Linq;

public partial class Front_MyAccountSettings : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            if (Request.QueryString["Popup"] == "1") {
                BindReferData();
                BindDealData();
                divSubcrigePopUp.Show();
            }
        }
    }

    protected void imgClose_Click(object sender, ImageClickEventArgs e) {
        Response.Redirect("~/MyAccountSettings.aspx?tab=0");
    }

    protected void Image3_Click(object sender, ImageClickEventArgs e) {
        divSubcrigePopUp.Show();
        To.Visible = true;
        From.Visible = true;
        Send.Visible = true;
        txtTo.Text = "";
        txtFrom.Text = "";
        txtErrorMsg.Text = "";
    }

    protected void btnReferSend_Click(object sender, EventArgs e) {
        try {
            string Url = "<a href=" + textLink.Text + ">" + textLink.Text + "</a>";
            //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
            //replacements.Add("<%From%>", txtFrom.Text);
            //replacements.Add("<%MailSubject%>", Url);
            //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl, Mailer.MessageTemplate.ReferLink, replacements, txtTo.Text.ToString(), "Refer Friends");
            //Mailer.InternalSend(confirmation, false);
            MailMessage Mail = new MailMessage();
            MailAddress ma = new MailAddress("mail@zupons.com", "Zupons");
            Mail.From = ma;
            Mail.To.Add(txtTo.Text.ToString());
            Mail.Subject = "Refer Friends";
            Mail.Body = Url;
            Mail.IsBodyHtml = true;
            try {
                SmtpClient smtpMailObj = new SmtpClient();
                smtpMailObj.Host = "mail.zupons.com";
                smtpMailObj.Send(Mail);
                // Response.Write("Your Message has been sent successfully");
            } catch (Exception ex) {
                //Response.Write("Message Delivery Fails");
            }
            txtErrorMsg.Visible = true;
            txtErrorMsg.Text = "Message Sent successfully.";
            txtTo.Text = "";
            txtFrom.Text = "";
            divSubcrigePopUp.Show();
        } catch (Exception ex) {
            txtErrorMsg.Visible = true;
            txtErrorMsg.Text = ex.Message;
        }
    }
    protected void BtnCancel_Click(object sender, EventArgs e) {
        To.Visible = false;
        From.Visible = false;
        Send.Visible = false;
        txtTo.Text = "";
        txtFrom.Text = "";
        txtErrorMsg.Text = "";
        divSubcrigePopUp.Show();
    }

    public void BindReferData() {
        string CityID = "";
        if (Request.Cookies["CityID"] != null) {
            CityID = Request.Cookies["CityID"].Value;
        }
        string UserID = Membership.GetUser().ProviderUserKey.ToString();
        var sub = new DealesDataContext();
        var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
        int DealID = 0;
        foreach (var item in query1) {
            DealID = Convert.ToInt32(item.DealID);
        }
        textLink.Text = "http://www.zupons.com/Default.aspx?ReferUserID=" + UserID + "&CityID=" + CityID.ToString();
        Twiteer.HRef = "http://twitter.com/home?status=https://www.zupons.com/Default.aspx?dealID=" + DealID.ToString();

        FaceBook.HRef = "http://www.facebook.com/sharer.php?u=https://www.zupons.com/Default.aspx?dealID=" + DealID.ToString();
    }

    public void BindDealData() {
        Session["username"] = Membership.GetUser().UserName.ToString();
        if (Session["username"] != null) {
            selectProduct();
            FillCrdeitAmount();
        }
    }

    public void selectProduct() {
        txtQuantity.Enabled = false;
        txtQuantity.Text = Session["Quantity"].ToString();
        var a = new DealesDataContext();
        var query = a.SelectProduct_Deal(Convert.ToInt32(Session["DealID"]));
        foreach (var item in query) {
            lblDescription.Text = item.ShortDescription;
            lblPrice.Text = (System.Math.Round(double.Parse(item.SellPrice.ToString()), 2)).ToString();
            lblTotalPrice.Text = (Convert.ToDouble(lblPrice.Text) * Convert.ToDouble(txtQuantity.Text)).ToString();
            lblYourTotal.Text = (Convert.ToDouble(lblPrice.Text) * Convert.ToDouble(txtQuantity.Text)).ToString();
            Session["TotalPrice"] = lblTotalPrice.Text;
            Session["Price"] = lblPrice.Text;
            Session["Quantity"] = txtQuantity.Text;
            ExpireDate.Text = " Expire: " + Convert.ToDateTime(item.ExpirationDate.ToString()).ToShortDateString();
            DealImg.ImageUrl = "Admin/ImageDisplay.aspx?DealID=" + item.DealID;

        }
        lblCredit.Text = "(0)";
        lblCredit.Visible = false;
        Credit.Visible = false;
        var query2 = new DealesDataContext();
        var CheckQtyQuery = query2.Select_MaxQtyFromDeal(Convert.ToInt32(Session["DealID"].ToString()));
        int LimitPerPerson = 0;
        foreach (var j in CheckQtyQuery) {
            LimitPerPerson = Convert.ToInt32(j.limitperperson);
        }
        if (LimitPerPerson == 1) {
            txtQuantity.Enabled = false;
        }
    }

    public void FillCrdeitAmount() {
        try {
            var credit = new DealesDataContext();
            //var Amount = credit.GetCreditAmount(new Guid(Membership.GetUser().ProviderUserKey.ToString()));
            //var Amount = Session["CreditAmount"] as IList<GetCreditAmountResult>;

            double money = 0.0;
            double referMoney = 0.0;
            ViewState["RferAmount"] = money;
            lblCredit.Text = "(0)";
            lblCredit.Visible = false;
            Credit.Visible = false;

            double creditAmount = 0.0;
            if (Session["CreditAmount"] is double) creditAmount = (double)Session["CreditAmount"];
            if (creditAmount > 0) {
                lblCredit.Text = "(" + creditAmount + ")";
                lblCredit.Visible = true;
                Credit.Visible = true;
            }

            //foreach (var item in Amount) {
            //    if (item.amount.ToString() != "0") {
            //        money = Convert.ToDouble(item.amount.ToString());
            //        if (money > 0) {
            //            //  lblFinalPrice.Visible = true;
            //            // lblFinalPrice.Text = "<b>Congratulations!</b> You have $" + money + " Zupons credit that will be applied to this purchase.";
            //            lblCredit.Text = "(" + money + ")";
            //            lblCredit.Visible = true;
            //            Credit.Visible = true;
            //        }
            //        ViewState["RferAmount"] = money;
            //        var referAmount = credit.GetReferAmount(new Guid(Membership.GetUser().ProviderUserKey.ToString()));
            //        foreach (var items in referAmount) {
            //            if (items.ReferAmount != null) {
            //                referMoney = Convert.ToDouble(items.ReferAmount.ToString());
            //                if ((money - referMoney) == 0.0) {

            //                    lblFinalPrice.Visible = false;
            //                    lblCredit.Visible = false;
            //                    Credit.Visible = false;
            //                } else {
            //                    //  lblFinalPrice.Visible = true;
            //                    //  lblFinalPrice.Text = "<b>Congratulations!</b> You have $" + (money - referMoney) + " Zupons credit that will be applied to this purchase.";
            //                    lblCredit.Text = "(" + (money - referMoney) + ")";
            //                    lblCredit.Visible = true;
            //                    Credit.Visible = true;
            //                }
            //                ViewState["RferAmount"] = (money - referMoney);
            //            }
            //        }
            //    }
            //}

            //double RferAmount = Convert.ToDouble(ViewState["RferAmount"].ToString());
            double TotalPrice = Convert.ToDouble(Session["TotalPrice"].ToString());
            if (TotalPrice > creditAmount) {
                Session["ReferAmount"] = creditAmount;
                Session["TotalPrice"] = Convert.ToDouble(Session["TotalPrice"].ToString()) - creditAmount;
                lblYourTotal.Text = Session["TotalPrice"].ToString();
                ViewState["CreditZero"] = null;
            } else {
                // Session["ReferAmount"] = Convert.ToDouble(ViewState["RferAmount"].ToString()) - Convert.ToDouble(Session["TotalPrice"].ToString());
                Session["ReferAmount"] = lblPrice.Text;
                lblYourTotal.Text = "0";
                Session["TotalPrice"] = "0";
                ViewState["CreditZero"] = 1;
            }

        } catch {
        }
    }
}
